;(function () {
  window.contract = {};
  contract.formatUnitData = function (id, fn) {
    var json = {
      cCode: '',
      mCode: '',
      deposit: '',
      contractName: '',
      zlfCompany: '',
      projectName: '',
      beginDate:'',
      endDate:'',
      st: [],
      ot: []
    };
    var st = [];
    var ot = [];
    hlhttp.get({
      url: 'contract',
      headers: {
        token: Cookies.get('token')
      },
      async: false,
      data: {
        id: id
      },
      success: function (res) {
        json.cCode = res.cCode || '--';
        json.mCode = res.mCode;
        json.zlfCompany = res.zlfCompany;
        json.deposit = res.deposit;
        json.contractId = res.id;
        json.projectId = res.projectId;
        json.beginDate = res.beginDate;
        json.endDate = res.endDate;
        json.contractName = res.name;
        json.projectName = res.projectName;
        json.attachmentNames = res.attachmentNames;
        json.attachmentUrls = res.attachmentUrls;
        json.compareRentAmtFlag = res.compareRentAmtFlag;

        var unit = res.units;
        for (var i = 0; i < unit.length; i++) {
          if (unit[i].businessType === 'ST') {
            st.push({
              buildingName: unit[i].buildingName,
              floorNum: unit[i].floorNum,
              unit: []
            })
          } else {
            ot.push({
              buildingName: unit[i].buildingName,
              floorNum: unit[i].floorNum,
              unit: []
            })
          }
        }
        //处理json.st
        var stArr = st.removeRepeat()
        if (stArr[0]  !== undefined) {
          for (var j = 0; j < stArr.length; j++) {
            json.st.push(stArr[j])
            for (var I = 0; I < unit.length; I++) {
              if (unit[I].buildingName == json.st[j].buildingName) {
                if (unit[I].floorNum == json.st[j].floorNum) {
                    json.st[j].unit.push(unit[I].unitName+'('+ hlNumberUtil.formatNum(unit[I].unitAcreage) +'m²)')
                }
              }
            }
          }
        }
        //处理json.ot
        var otArr = ot.removeRepeat()
        if (otArr[0] !== undefined) {
          for (var z = 0; z < otArr.length; z++) {
            json.ot.push(otArr[z])
            for (var I = 0; I < unit.length; I++) {
              if (unit[I].buildingName == json.ot[z].buildingName) {
                if (unit[I].floorNum == json.ot[z].floorNum) {
                    json.ot[z].unit.push(unit[I].unitName+'('+ hlNumberUtil.formatNum(unit[I].unitAcreage) +'m²)')
                }
              }
            }
          }
        }

        fn && fn(json)
      }
    })
  }

  Array.prototype.removeRepeat = function () {
    var res = [this[0]];
    for (var i = 1; i < this.length; i++) {
      var repeat = false;
      for (var j = 0; j < res.length; j++) {
        if (this[i].buildingName == res[j].buildingName && this[i].floorNum == res[j].floorNum) {
          repeat = true;
        }
      }
      if (!repeat) {
        res.push(this[i]);
      }
    }
    return res;
  };
}())
